package com.aliang.shopping.model.converter;

import com.aliang.shopping.model.dto.common.UpdateStatusDTO;
import com.aliang.shopping.model.dto.notice.NoticeImportDTO;
import com.aliang.shopping.model.dto.notice.NoticeInsertDTO;
import com.aliang.shopping.model.dto.notice.NoticeUpdateDTO;
import com.aliang.shopping.model.po.Notice;
import com.aliang.shopping.model.vo.notice.NoticeExportVO;
import com.aliang.shopping.model.vo.notice.NoticeVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;

/**
 * 公告数据转换器
 */
@Mapper(componentModel = "Spring")
public interface NoticeConverter {

    /**
     * 将公告插入数据传输对象转换为公告实体
     *
     * @param insertDTO
     * @return
     */
    Notice toPoByInsertDTO(NoticeInsertDTO insertDTO);

    /**
     * 将公告更新数据传输对象转换为公告实体
     *
     * @param updateDTO
     * @return
     */
    Notice toPoByUpdateDTO(NoticeUpdateDTO updateDTO);

    /**
     * 将公告更新状态数据传输对象转换为公告实体
     *
     * @param updateDTO
     * @return
     */
    Notice toPoByUpdateStaticDTO(UpdateStatusDTO updateDTO);

    /**
     * 将公告导入数据传输对象转换为公告实体
     *
     * @param vo
     * @return
     */
    @Mappings({
            @Mapping(target = "userId", expression = "java(getRandomUserId())"),
            @Mapping(target = "formNum", expression = "java(getRandomToNum())"),
    })
    Notice toPoByImportDTO(NoticeImportDTO vo);

    default Long getRandomUserId() {
        return (long) (Math.random() * 100000000);
    }

    default Integer getRandomToNum() {
        return (int) (Math.random() * 2);
    }

    List<Notice> toPoListByImportDTOList(List<NoticeImportDTO> list);

    /**
     * 将公告展示对象转换为公告导出对象
     *
     * @param vo
     * @return
     */
    @Mappings({
            @Mapping(target = "form", expression = "java(CommonConverter.getMsg(vo.getFormNum(),\"商户端\",\"管理端\"))"),
            @Mapping(target = "to", expression = "java(CommonConverter.getMsg(vo.getFormNum(),\"商户端\",\"用户端\"))"),
            @Mapping(target = "status", expression = "java(CommonConverter.getMsg(vo.getFormNum(),\"正常\",\"禁用\"))"),
    })
    NoticeExportVO toExportVO(NoticeVO vo);

    List<NoticeExportVO> toImportDTOList(List<NoticeVO> list);


    NoticeVO toVO(Notice po);

    List<NoticeVO> toVOList(List<Notice> list);
}
